package admin.servlet;

import admin.dao.Addao;
import admin.entity.Adenty;
import admin.utils.JBDCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

@WebServlet("/updata")
public class Upservlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String cid=req.getParameter("cid");
        String cpwd=req.getParameter("cpwd");
        String cname=req.getParameter("cname");
        String csex=req.getParameter("csex");
        String ccall=req.getParameter("ccall");

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out =resp.getWriter();

        JBDCUtil db=new JBDCUtil();
        Adenty adenty=new Adenty();

        adenty.setCid(cid);
        adenty.setCpwd(cpwd);
        adenty.setCname(cname);
        adenty.setCsex(csex);
        adenty.setCcall(ccall);

        Addao dao=new Addao();


        try {
            Connection conn=db.getConn();
            if(dao.updata(conn,adenty)){
                out.println(dao.updata(conn,adenty));
                out.println("<h1>修改成功</h1>");
                resp.setHeader("Refresh", "1;url=Adlist.jsp");

            }else {
                out.println(dao.updata(conn,adenty));
                out.println("<h1>没有此账号,修改失败</h1>");
                resp.setHeader("Refresh", "1;url=upadinfo.jsp");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
